package com.microinvest.service.impl;
import com.microinvest.util.RedisUtil;
import com.microinvest.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    private static final long CODE_EXPIRY_TIME = 5 * 60; // 验证码有效期5分钟

    private final RedisUtil redisUtil;

    @Autowired
    public RedisServiceImpl(RedisUtil redisUtil){
        this.redisUtil=redisUtil;
    }

    //邮箱验证码信息存入redis
    @Override
    public void storeVerificationCode(String email, String code) {
        String key = "verification:code:" + email;
        redisUtil.set(key, code, CODE_EXPIRY_TIME, TimeUnit.SECONDS);
    }

    //验证邮箱对应的code是否正确
    @Override
    public boolean validateVerificationCode(String email, String code) {
        String key = "verification:code:" + email;
        String storedCode = redisUtil.get(key);
        if (storedCode.equals(code)) {
            redisUtil.delete(key); // 验证成功后删除验证码
            return true;
        }
        return false;
    }

//    @Override
//    public void removeVerificationCode(String email) {
//        String key = "verification:code:" + email;
//        redisUtil.delete(key);
//    }
}
